package com.zengqingfa.thread.demo3;

/**
 * @fileName: ThreadTest1
 * @author: zengqf3
 * @date: 2020-10-7 11:51
 * @description:
 */
public class StopThread implements Runnable {

    //定义线程标识
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 线程执行 "+i++);
        }
    }

    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) throws InterruptedException {
        StopThread test2 = new StopThread();
        //开启一个线程
        new Thread(test2).start();
        Thread.sleep(2000);
        //关闭线程
        test2.stop();

    }
}
